package com.zmn.oms.services.impl.ins;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.oms.model.entity.ins.InsOrderSync;
import com.zmn.oms.persistence.interfaces.ins.InsOrderSyncYeyxDao;
import com.zmn.oms.persistence.interfaces.ins.InsOrderSyncZmnDao;
import com.zmn.oms.services.interfaces.ins.InsOrderSyncService;
import com.zmn.oms.services.interfaces.orderencrypt.InsOrderEncryptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

/**
 * 类描述：
 *
 * @author lhl
 * @date 2020/06/01 19:01
 */
@Service("insOrderSyncService")
public class InsOrderSyncServiceImpl implements InsOrderSyncService {

    @Resource
    private InsOrderSyncZmnDao insOrderSyncZmnDao;
    @Resource
    private InsOrderSyncYeyxDao insOrderSyncYeyxDao;
    @Autowired
    private InsOrderEncryptService insOrderEncryptService;

    @Override
    public List<InsOrderSync> listByOrderId(String code) {
        List<InsOrderSync> list = new LinkedList<>();

        List<InsOrderSync> zmnInsOrderSyncs = insOrderSyncZmnDao.listByOrderId(code);
        if (!CollectionUtils.isEmpty(zmnInsOrderSyncs)) {
            for (InsOrderSync item : zmnInsOrderSyncs) {
                list.add(item);
            }
        }

        List<InsOrderSync> yeyxInsOrderSyncs = insOrderSyncYeyxDao.listByOrderId(code);
        if (!CollectionUtils.isEmpty(yeyxInsOrderSyncs)) {
            for (InsOrderSync item : yeyxInsOrderSyncs) {
                list.add(item);
            }
        }

        return insOrderEncryptService.batchDecryptInsOrderSync(list);
    }

    @Override
    public List<InsOrderSync> listZmnNoSynced(Integer rows) {
        List<InsOrderSync> insOrderSyncs = insOrderSyncZmnDao.listNoSynced(rows);
        return insOrderEncryptService.batchDecryptInsOrderSync(insOrderSyncs);
    }

    @Override
    public List<InsOrderSync> listYeyxNoSynced(Integer rows) {
        List<InsOrderSync> insOrderSyncs = insOrderSyncYeyxDao.listNoSynced(rows);
        return insOrderEncryptService.batchDecryptInsOrderSync(insOrderSyncs);
    }

    @Override
    public void updateSyncedByKey(InsOrderSync insOrderSync) {
        Integer platWork = insOrderSync.getPlatWork();
        if (platWork == null) {
            insOrderSyncZmnDao.updateSyncedByKey(insOrderSync.getInsSyncId());
            insOrderSyncYeyxDao.updateSyncedByKey(insOrderSync.getInsSyncId());
            return;
        }

        if (Objects.equals(GlobalConsts.PLAT_MARK_ZMN, platWork)) {
            insOrderSyncZmnDao.updateSyncedByKey(insOrderSync.getInsSyncId());
        }
        else {
            insOrderSyncYeyxDao.updateSyncedByKey(insOrderSync.getInsSyncId());
        }
    }

    @Override
    @Transactional
    public void updateZmnSyncedByKeys(List<Long> insSyncIdList) {
        insOrderSyncZmnDao.updateSyncedByKeys(insSyncIdList);
    }

    @Override
    @Transactional
    public void updateYeyxSyncedByKeys(List<Long> insSyncIdList) {
        insOrderSyncYeyxDao.updateSyncedByKeys(insSyncIdList);
    }

    @Override
    public int insertInsOrderSync(InsOrderSync insOrderSync) {
        int count = 0;

        InsOrderSync insOrderSyncCopy = insOrderEncryptService.encryptInsOrderSync(insOrderSync);
        List<InsOrderSync> list = new LinkedList<>();
        list.add(insOrderSyncCopy);

        Integer platWork = insOrderSync.getPlatWork();
        if (Objects.equals(GlobalConsts.PLAT_MARK_ZMN, platWork)) {
            count = insOrderSyncZmnDao.insertInsOrderSyncList(list);
        }
        else {
            count = insOrderSyncYeyxDao.insertInsOrderSyncList(list);
        }

        insOrderSync.setInsSyncId(insOrderSyncCopy.getInsSyncId());

        return count;
    }

    @Override
    public int insertInsOrderSyncList(List<InsOrderSync> insOrderSyncList) {
        int count = 0;
        if (insOrderSyncList == null || insOrderSyncList.isEmpty()) {
            return count;
        }

        List<InsOrderSync> insOrderSyncsCopy = insOrderEncryptService.batchEncryptInsOrderSync(insOrderSyncList);

        List<InsOrderSync> zmnList = new LinkedList<>();
        List<InsOrderSync> yeyxList = new LinkedList<>();

        for (InsOrderSync item : insOrderSyncsCopy) {
            Integer platWork = item.getPlatWork();
            if (Objects.equals(GlobalConsts.PLAT_MARK_ZMN, platWork)) {
                zmnList.add(item);
            }
            else {
                yeyxList.add(item);
            }
        }

        if (zmnList.size() > 0) {
            int countZmn = insOrderSyncZmnDao.insertInsOrderSyncList(zmnList);
            count += countZmn;
        }

        if (yeyxList.size() > 0) {
            int countYeyx = insOrderSyncYeyxDao.insertInsOrderSyncList(yeyxList);
            count += countYeyx;
        }

        return count;
    }
}
